#include    "sheetsprite.h"
#include    <stdlib.h>


const Vec2D SheetSprite::DefaultVelocity = {15,0};
const Vec2D SheetSprite::ZeroVelocity = {0,0};
SheetSprite::SheetSprite():
    m_node(0),
    m_towardRight(true),
    m_action(ACTION::NOPIC),
    m_lastAction(ACTION::NOPIC),
    m_velocity(ZeroVelocity),
    m_render(0),
    m_showCount(0)
{
    m_instruction.clear();
    m_pictures.clear();
    m_node = new MovableNode();
    m_node->setPosition(0,600 - 150);
    m_render = new SpriteSheetRender();
}

SheetSprite::~SheetSprite()
{
    SAFE_DELETE_OBJ(m_node);
    SAFE_DELETE_OBJ(m_render);
    for(auto it = m_pictures.begin(); it != m_pictures.end(); it++)
    {
        delete it->second;
    }
    m_pictures.clear();
}

void SheetSprite::loadResource(ACTION action,const char* name,const char* resourcePath)
{
    std::string n(name);
    std::string path(resourcePath);
    std::string pngSuffix(".png");
    std::string jsonSuffix(".json");
    std::string textureName = path + n + pngSuffix;
    std::string jsonName = path + n + jsonSuffix;

    SpriteSheetPicture* pic = new SpriteSheetPicture();
    pic->loadTexture(textureName.c_str());
    pic->parseJson(jsonName.c_str());
    m_pictures[action] =  pic;
}

void SheetSprite::setAction(ACTION action)
{
    if(action == RUN && m_action != ATTACK)
    {
        m_node->move(m_towardRight? 15 : -15 ,0);
    }

    if(m_action == action || m_action == JUMP || m_action == ATTACK){return;}

    switch(action)
    {
    case JUMP:
        {
            m_instruction.push_back([=](){m_action = action;
                                        m_render->setSpriteSheetPicture(
                                                                        m_pictures[m_action]
                                                                        );}
                                        );
            for(int i = 0 ; i < 5; i ++){m_instruction.push_back([&](){ m_node->move(0,-10);});}
            for(int i = 0 ; i < 5; i ++){m_instruction.push_back([&](){ m_node->move(0,10);});}
            m_instruction.push_back([=](){m_action = IDLE;
                                        m_render->setSpriteSheetPicture(
                                                                        m_pictures[m_action]
                                                                        );}
                                        );
        }
        break;
    case RUN:
        {
            m_instruction.push_back([=](){m_action = action;
                                        m_render->setSpriteSheetPicture(
                                                                        m_pictures[m_action]
                                                                        );}
                                        );
        }
        break;
    case ATTACK:
        {
            m_instruction.push_back([=](){m_action = action;
                                        m_render->setSpriteSheetPicture(
                                                                        m_pictures[m_action]
                                                                        );}
                                        );
            for(int i = 0 ; i < 5; i ++){m_instruction.push_back([&](){ });}
            for(int i = 0 ; i < 5; i ++){m_instruction.push_back([&](){ });}
            m_instruction.push_back([=](){m_action = IDLE;
                                        m_render->setSpriteSheetPicture(
                                                                        m_pictures[m_action]
                                                                        );}
                                        );
        }
        break;
    case IDLE:
        {
            m_instruction.push_back([=](){  m_action = action;
                                            m_render->setSpriteSheetPicture(
                                                                        m_pictures[m_action]
                                                                        );
                                        });
        }
        break;
    default:
        break;
    }
}

void SheetSprite::update(TimeSeconds time)
{
    auto it = m_instruction.begin();
    if(it != m_instruction.end())
    {
        (*it)();
        m_instruction.erase(it);
    }

    m_render->update();
}

void SheetSprite::render()
{
    const Point& p = m_node->getPosition();
    m_towardRight ? m_render->render(p.x,p.y) : m_render->renderReverse(p.x,p.y);
}
